package com.yylending.las.lending;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.statistics.StatService;
import com.yylending.las.Constants;
import com.yylending.las.R;
import com.yylending.las.StatIDSet;
import com.yylending.las.base.BaseFragment;
import com.yylending.las.common.widgets.RedDotView;
import com.yylending.las.common.widgets.TelephoneTextView;
import com.yylending.las.lending.quota.myquota.MyQuotaModel;
import com.yylending.las.main.MainController;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 借钱页面-零额度
 * Created by laixj on 2017/5/22.
 * Copyright ©2017 www.yylending.com. All Rights Reserved.
 */

public class ZeroQuotaFragment extends BaseFragment {

	static final String ARG_APPLY_STATUS = "arg_apply_status";

	Unbinder unbinder;
	@BindView(R.id.title_tv)
	TextView mTitleTv;
	@BindView(R.id.service_phone_tv)
	TelephoneTextView mServicePhoneTv;
	@BindView(R.id.title_left_iv)
	ImageView mTitleLeftIv;
	@BindView(R.id.title_right_iv)
	ImageView mTitleRightIv;
	@BindView(R.id.lending_notice_tv)
	TextView mLendingNoticeTv;
	@BindView(R.id.no_quota_tv)
	TextView mNoQuotaTv;
	@BindView(R.id.notice_phone_divider)
	TextView mNoticePhoneDivider;

	MyQuotaModel.ApplyStatus mApplyStatus;
	@BindView(R.id.red_dot_view)
	RedDotView mRedDotView;
	@BindView(R.id.layout_title_bar)
	RelativeLayout mLayoutTitleBar;

	public static BaseFragment newInstance(MyQuotaModel.ApplyStatus status) {
		BaseFragment fragment = new ZeroQuotaFragment();
		Bundle args = new Bundle();
		args.putParcelable(ARG_APPLY_STATUS, status);
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.view_stub_no_limit, container, false);
		unbinder = ButterKnife.bind(this, rootView);
		Bundle bundle = getArguments();
		if (bundle != null) {
			mApplyStatus = bundle.getParcelable(ARG_APPLY_STATUS);
		}
		initView();
		return rootView;
	}

	private void initView() {
		mLayoutTitleBar.setBackgroundResource(0);
		mLayoutTitleBar.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.transparent));
		mTitleTv.setText(R.string.tab_lend);
		mTitleTv.setTextColor(ContextCompat.getColor(getActivity(), R.color.color_333333));
		mTitleLeftIv.setImageResource(R.drawable.ic_help_black);
		mTitleRightIv.setImageResource(R.drawable.ic_message_black);
		mServicePhoneTv.setPageNo(Constants.PAGE_NO_MAIN_ACTIVITY);

		if (null != mApplyStatus) {
			if (Constants.QUOTA_STATUS_REFUSED.equals(mApplyStatus.amountState)) {
				mNoQuotaTv.setText(getString(R.string.no_quota_refused, getString(R.string.app_name)));
			} else if (Constants.LOAN_STATUS_REFUSED.equals(mApplyStatus.loanState)) {
				StatService.onEvent(getContext(), StatIDSet.EVENT_ID_AUDIT_NOT_PASSED);
				mNoQuotaTv.setText(getString(R.string.lending_refused, getString(R.string.app_name)));
				mLendingNoticeTv.setVisibility(View.GONE);
				mNoticePhoneDivider.setVisibility(View.GONE);
			}
		}
		MainController.initRedDot(mRedDotView);
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return null;
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		unbinder.unbind();
	}

	@OnClick({R.id.title_left_iv, R.id.title_right_iv, R.id.lending_notice_tv})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.title_left_iv:
				EventAgent.post(new QuotaEvent.PageJumpEvent(QuotaEvent.PageJumpEvent.QUOTA_JUMP_HELP_PAGE, Constants.PAGE_NO_MAIN_ACTIVITY));
				break;
			case R.id.title_right_iv:
				EventAgent.post(new QuotaEvent.PageJumpEvent(QuotaEvent.PageJumpEvent.QUOTA_JUMP_MESSAGE_PAGE, Constants.PAGE_NO_MAIN_ACTIVITY));
				MainController.hideRedDot(mRedDotView);
				break;
			case R.id.lending_notice_tv:
				EventAgent.post(new QuotaEvent.ShowLoanNoticeEvent(QuotaEvent.ShowLoanNoticeEvent.QUOTA_SHOW_LOAN_NOTICE, Constants.PAGE_NO_MAIN_ACTIVITY));
				break;
		}
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onRedDotShowEvent(QuotaEvent.ShowRedDotEvent event) {
		mRedDotView.setVisibility(View.VISIBLE);
	}
}